package com.sg.java.apps;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.google.gson.JsonObject;
import com.sg.java.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试webservice
 */
public class TestWS {

    private static final Logger log = LoggerFactory.getLogger(TestWS.class);

    public static void main(String[] args) {
        final String access_token_url = "http://25.64.40.150:32100/baseCenter/oauth2/accessToken";
        final HttpRequest access_token_req = HttpUtil.createPost(access_token_url).body(
                "{\n" +
                "\t\"password\":\"\",\n" +
                "\t\"grant_type\":\"client_credentials\",\n" +
                "\t\"client_secret\":\"PwVO09sLmDOl1mpLGdWMcRYkfR7DPFmUMWPsI4n5qqcWSadcX0we2wffjyTUYGsK\",\n" +
                "\t\"client_id\":\"60362a27037611ed904c0255ac10010b\",\n" +
                "\t\"username\":\"\"\n" +
                "}"
        );
        log.info("access_token_req:{}", access_token_req);

        try (final HttpResponse access_token_resp = access_token_req.execute()) {
            log.info("access_token_resp:{}", access_token_resp);
            final String body = access_token_resp.body();
            log.info("access_token_resp body:{}", body);
            final JsonObject jo = JSONUtils.toJsonObject(body);
            final String access_token = jo.get("access_token").getAsString();
            log.info("access_token:{}", access_token);

            String url = "http://25.64.40.150:32100/yj-pms-authority/iscorglocext/getAllDeptIdAndName";
            final HttpRequest req = HttpUtil.createPost(url).header("x-token", access_token);
            log.info("req:{}", req);
            HttpResponse resp = req.execute();
            log.info("resp:{}", resp);
            JsonObject jo_2 = JSONUtils.toJsonObject(resp.body());
            resp.close();
            log.info("result:{}", jo_2);
        }

    }

}
